﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace StationUpdate
{
    public class SysLog
    {
        /// <summary>
        /// 日志目录地址
        /// </summary>
        public static string logPath = "";
        /// <summary>
        /// 日志保留的天数，默认值为60天。
        /// </summary>
        public static int SaveLogDay = 60;
        /// <summary>
        /// 写日志文件--日志路径在web.config配置文件里，日志名称为“******Log.txt”
        /// 无论是否启用日志模式，都会将日志内容写入文件
        /// </summary>
        /// <param name="strPath"></param>
        /// <param name="strFileName"></param>
        /// <param name="strLogMessage"></param>
        public static void WriteTxtLog(string strLogMessage)
        {
            try
            {
                WriteTxtLog(logPath, "Log.txt", strLogMessage);
            }
            catch (Exception ex)
            { }
        }

        /// <summary>
        /// 写文本日志，自定义文件
        /// </summary>
        /// <param name="strLogDirectoryPath"></param>
        /// <param name="strLogFileName"></param>
        /// <param name="strMsg"></param>
        public static void WriteTxtLog(string strLogDirectoryPath, string strLogFileName, string strMsg)
        {
            log(strLogDirectoryPath, strLogDirectoryPath + "\\" + strLogFileName, strMsg, strLogFileName);
        }

        /// <summary>
        /// 写日志文件
        /// </summary>
        /// <param name="strLogDirectoryPath"></param>
        /// <param name="sPath"></param>
        /// <param name="slog"></param>
        /// <param name="strLogFileName"></param>
        private static void log(string strLogDirectoryPath, string sPath, string slog, string strLogFileName)
        {
            FileStream fs = null;
            StreamWriter w = null;
            FileInfo fi = null;
            try
            {
                CheckLogFilePath(strLogDirectoryPath);

                fs = new FileStream(sPath, FileMode.OpenOrCreate, FileAccess.ReadWrite);

                w = new StreamWriter(fs);
                w.BaseStream.Seek(0, SeekOrigin.End);
                w.Write("{0}	{1}	{2}\r\n", DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString(), slog);
                w.Flush();

                w.Close();
                fs.Close();

                fi = new FileInfo(sPath);

                if (fi.Length > 800000)
                {
                    string newFileName = strLogDirectoryPath + "\\" + string.Format("{0}{1}{2}{3}{4}{5}" + strLogFileName,
                        DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
                    System.IO.File.Move(sPath, newFileName);
                }
                //删除两个月之前的数据
                DeleteOldFile(SaveLogDay, strLogDirectoryPath);
            }
            catch (Exception e)
            {
                string se = e.Message.ToString();
            }
            finally 
            {
                if (fs != null) 
                {
                    fs.Close();
                    fs.Dispose();
                }
                if (w != null) 
                {
                    w.Close();
                    w.Dispose();
                }
            }
        }

        public static void CheckLogFilePath(string strPath)
        {
            try
            {
                DirectoryInfo dir = new DirectoryInfo(strPath);
                if (!dir.Exists)
                {
                    // 如果文件夹不存在，那么就创建
                    dir.Create();
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// 根据配置参数删除指定日期以前的文件
        /// </summary>
        public static void DeleteOldFile(Int32 LogMinDays, string LogFilePath)
        {
            try
            {
                DirectoryInfo dirInfo = new DirectoryInfo(LogFilePath);

                foreach (FileInfo fInfo in dirInfo.GetFiles("*.*"))
                {
                    TimeSpan timeEscape = DateTime.Now - fInfo.CreationTime;
                    if (timeEscape.Days > LogMinDays)
                    {
                        System.IO.File.Delete(fInfo.FullName);
                    }
                }
            }
            catch (Exception e)
            {
                e.Message.ToString();
            }
        }
    }
}
